﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace mc_tester
{
    //ez a User Control-on keresztül állítgathatunk egy portot
    public partial class PortControl : UserControl
    {
        //ehhez a porthoz tartozik ez a PortControl
        private Port _port;
        private Device _device;
        //színek
        private Color ColorDirectionIN=Color.LightGray;
        private Color ColorDirectionOUT =Color.GreenYellow;
        
        private Color ColorStateHIGH=Color.LightYellow;
        private Color ColorStateLOW=Color.LightGray;

        private Color ColorLEDON = Color.Tomato;
        private Color ColorLEDOFF=Color.White;

        //konstruktor
        public PortControl()
        {
            InitializeComponent();
        }
        public PortControl(Port p, Device d)
        {
            InitializeComponent();
            _port = p;
            _device = d;
            this.nameLabel.Text = _port.name;

            levelCheckBox.Text = Port.LEVELS.LOW.ToString();
            levelCheckBox.BackColor = ColorStateLOW;

            directionCheckBox.BackColor = ColorDirectionOUT;
            directionCheckBox.Text = Port.DIRECTIONS.OUT.ToString();

            if (_port.adcMode)
            {
                adcBeolvasButton.Visible = true;
                adcErtek.Visible = true;
            }
            else
            {
                adcBeolvasButton.Visible = false;
                adcErtek.Visible = false;
            }
        }

        //ez színezi a ledeket
        private void stateCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (stateCheckBox.Checked)
            {
                stateCheckBox.BackColor = ColorLEDOFF;
            }
            else
            {
                stateCheckBox.BackColor = ColorLEDON;
            }
        }
        
        //port level állítás
        private void levelCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (levelCheckBox.Checked)
            {
                _port.portLevel = Port.LEVELS.LOW;
                levelCheckBox.BackColor = ColorStateLOW;
                levelCheckBox.Text = Port.LEVELS.LOW.ToString(); ;
            }
            else
            {
                _port.portLevel = Port.LEVELS.HIGH;
                levelCheckBox.BackColor = ColorStateHIGH;
                levelCheckBox.Text = Port.LEVELS.HIGH.ToString(); ;
            }

                _port.sendMessage(_port.portLevel.ToString()+" "+ _port.name);
        }
        
        //port in-out állítás
        private void directionCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (directionCheckBox.Checked)
            {
                _port.portDirection = Port.DIRECTIONS.IN;
                directionCheckBox.BackColor = ColorDirectionIN;
                directionCheckBox.Text = Port.DIRECTIONS.IN.ToString();
            }
            else
            {
                _port.portDirection = Port.DIRECTIONS.OUT;
                directionCheckBox.BackColor = ColorDirectionOUT;
                directionCheckBox.Text = Port.DIRECTIONS.OUT.ToString();
            }
            _port.sendMessage(_port.portDirection.ToString()+" "+_port.name);
        }

        //a port státusát kérjük le soros porton keresztül
        private void actionButton_Click(object sender, EventArgs e)
        {
                _port.sendMessage(Port.COMMANDS.GET.ToString()+ " " + _port.name);
        }
        
        // led beállítás
        public void setLed(Port.LEVELS b)
        {
            if (b==Port.LEVELS.HIGH)
                stateCheckBox.Checked = false;
            else
                stateCheckBox.Checked = true;
        }

        //kérjük az ADC értéket
        private void adcBeolvasButton_Click(object sender, EventArgs e)
        {
                _port.sendMessage(Port.COMMANDS.ADC.ToString() + " " + _port.name);
        }
        
        //ADC szám kiírása
        public void setADCvalue(int i){
            adcErtek.Text=i.ToString();
        }

        private void timerButton_Click(object sender, EventArgs e)
        {
            TimerDialog td = new TimerDialog(_port,_device);
            td.ShowDialog();
        }
        public void settimerbuttoncolor(Color c)
        {
            timerButton.ForeColor = c;
        }
    }//end PortControl class

}
